//
//  KL2.m
//  NetTest
//
//  Created by mac on 12-5-14.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "KL2.h"
#import "TicketResultService.h"

@implementation KL2

-(id)init{
    if ([super init]) {
        ticketCount = 2;
        ticketGroupCount = 2;
        numGroupCount = 20*19/4;
        
        printCount=2;
        printTj=20;
        
        service=[[TicketResultService alloc] init];
        printArray=[[NSMutableArray alloc] init];
    }
    return self;
}

- (BOOL)printRandomNumGroup {
    bool blFind=false;

    for (int i=0; i<numGroupCount; i++) {
        NSMutableArray *num=(NSMutableArray *)[numgroup objectAtIndex:i];
        
        NSMutableArray *group1=[num objectAtIndex:0];
        
        NSMutableArray *group2=[num objectAtIndex:1];
        
        NSString *strwhere=[NSString stringWithFormat:@" (result like \'%%%@%%\' and result like \'%%%@%%\') or (result like \'%%%@%%\' and result like \'%%%@%%\')",[group1 objectAtIndex:0],[group1 objectAtIndex:1],[group2 objectAtIndex:0],[group2 objectAtIndex:1]];
        
        NSMutableArray* arr=[service getCollection:strwhere orderby:@"sectionID desc" startIndex:0 count:1];
        
        if (arr!=nil && [arr count]>0) {
            
            TicketResult* r=[arr objectAtIndex:0];
            
            int tcount= [service getCountBystrwhere:[NSString stringWithFormat:@"sectionID > \'%@\'" ,r.sectionID]];
            if (tcount>printTj) {
                
                blFind=true;
 
                bool hasrepeat = false;
                if ([printArray count]>=1) {
                    for (ForecastTick *ft in printArray ) {
                        
                        if ([ft.tick.sectionID isEqualToString:r.sectionID]) {
                            hasrepeat=true;
                            break;
                        }
                    }
                }
                if (hasrepeat) {
                    continue;
                }
                ForecastTick * tick=[[ForecastTick alloc] init];
                [tick setTick:r];
                tick.spanCount=tcount;
                tick.group=num;
                [tick printLog];
                [printArray addObject:tick];
                [tick release];
//                NSLog(@"i = %i  %@ %@ - %@ %@  期号:%@ 号码:%@ 总共:%i",i,[group1 objectAtIndex:0],[group1 objectAtIndex:1],[group2 objectAtIndex:0],[group2 objectAtIndex:1], r.sectionID,r.result,tcount);
            }
        }
        else{
            NSLog(@"i = %i  %@ %@ - %@ %@ ",i,[group1 objectAtIndex:0],[group1 objectAtIndex:1],[group2 objectAtIndex:0],[group2 objectAtIndex:1]);
            blFind =true;
            NSLog(@"数据库中无此数据!");
        }
    }
    return blFind;
}

-(void)createNumGroup{
    bool blFind=true;
    int index=0;
    [[Sqlite3Helper Instance] openDB];
    do {
        
        NSLog(@"第%i圈:",(++index));
        
        NSMutableArray* tickgroup=[NSMutableArray array];//190组数据
        
        for (int i=1; i<=19; i++) {
            for (int j=i+1; j<=20; j++) {
                NSMutableArray *group=[NSMutableArray array];
                
                [group addObject:[NSString stringWithFormat:@"%02d",i]];
                
                [group addObject:[NSString stringWithFormat:@"%02d",j]];
                
                [tickgroup addObject:group];
            }
        }
        
        numgroup=[[NSMutableArray array] retain];
        
        for (int i=0; i<numGroupCount; i++) {
            
            NSMutableArray *arr=[NSMutableArray array];
            
            for (int j=0; j<ticketGroupCount; j++) {
                int gpcount=[tickgroup count];
                

                time_t t=time(NULL);
                srand(t);
                
                t=random()%t;
                
                FCLOG(@"seed = %ld",t);
                
                srand(t);
                int r = rand()%gpcount;
                
                [arr addObject:[tickgroup objectAtIndex:r]];
                
                [tickgroup removeObjectAtIndex:r];
            }
            
            [numgroup addObject:arr];
        }
    
        blFind=[self printRandomNumGroup];
        if (blFind) {
            index=0;
        }
        [numgroup release];
    } while ([printArray count]<printCount && index<500);
    
    [[Sqlite3Helper Instance] closeDB];
    
    NSLog(@"print Count = %i printArray count = %i index = %i ",printCount,[printArray count],index);
    for (ForecastTick * f in printArray ) {
        [f printLog];
    }
}

-(void)dealloc{
    
    [service release];
    //[numgroup release];
    
    [super dealloc];
}

@end
